package org.skatiger.jrove.spring.messages;

import javax.el.ELContext;
import javax.el.ELContextEvent;
import javax.el.ELContextListener;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.spring.view.JroveView;
import org.skatiger.jrove.util.AssertUtil;
import org.springframework.web.servlet.support.RequestContext;

public class SpringMessageBeanExposeListener implements ELContextListener {
    public static final String DEFAULT_BEAN_NAME = "i18n";
    private String beanName = DEFAULT_BEAN_NAME;

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public void contextCreated(ELContextEvent event) {
        ELContext elctx = event.getELContext();
        ViewContext viewContext = ViewContext.findViewContext(elctx);
        RequestContext requestContext = (RequestContext) viewContext
                .getAttribute(JroveView.REQUEST_CONTEXT_VIEW_ATTRIBUTE);
        AssertUtil.notNull(requestContext);
        SpringMessageBean bean = new SpringMessageBean(requestContext);
        viewContext.setAttribute(beanName, bean);
    }

}
